<?php

class Store_stockApp extends BackendApp {

    var $_storeStock_mod;

    /**
     * @var string 仓库model
     */
    protected $_store_stock_mod;


    function __construct() {
        $this->CouponApp();
    }

    function CouponApp() {
        parent::BackendApp();
        $this->_store_stock_mod = & m('storestock');
    }

    /**
     * 默认列表
     */
    function index()
    {
        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 's.store_name',
                // name值必须设置，否则不会出现$conditions条件，大坑...
                'name' => 'store_name',
                'equal' => 'like',
            ),
            array(
                'field' => 'coupon_codes.not_before',
                'name' => 'not_before',
                'equal' => '>=',
            ),
            array(
                'field' => 'coupon_codes.not_after',
                'name' => 'not_after',
                'equal' => '<=',
            ),
        ));

        // 处理分页
        $page = $this->_get_page(20);
        $coupon_codes_list = $this->_store_stock_mod->find(array(
            'conditions' => "1=1 " . $conditions,
            'limit' => $page['limit'],
            'count' => true,
        ));
        $page['item_count'] = $this->_store_stock_mod->getCount();
        $this->_format_page($page);
        $this->assign('filtered', $conditions ? 1 : 0); //是否有查询条件
        $this->assign('page_info', $page);
        $this->assign('coupon_codes_list', $coupon_codes_list);
        // 引入前端资源
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
        // 显示模板
        $this->display('store_stock.list.html');
    }


    /**
     *    编辑
     *
     * @return bool
     * @author    Hyber
     */
    function edit()
    {
        $storestock_id = isset($_GET['storestock_id']) ? intval($_GET['storestock_id']) : 0;
        if (!$storestock_id) {
            $this->show_warning('data_not_exist');
            return;
        }
        if (!IS_POST) {

            $find_data = $this->_store_stock_mod->find($storestock_id);
            if (empty($find_data)) {
                $this->show_warning('data_not_exist');
                return;
            }
            $coupon_codes = current($find_data);

            $this->assign("storestock_id", $storestock_id);
            // 引入前端资源

            $this->assign('coupon_codes', $coupon_codes);
            $this->display('store_stock.add-edit.form.html');
        } else {

            // 验证结果
            $check_result = $this->checkPostParams();
            if ($check_result === false) {
                return false;
            }

            // 接收参数值，以下为必传参数
            $data = array(
                'stock_address' => $_POST['stock_address'],
                'arrival_time' => $_POST['arrival_time'],
            );

            $this->_store_stock_mod->edit($storestock_id, $data);
            if ($this->_store_stock_mod->has_error()) {
                $this->show_warning($this->_store_stock_mod->get_error());
                return;
            } else {
                $this->admin_log("修改仓库信息", 1);
            }          

            $this->show_message('保存成功', '返回列表', 'index.php?app=store_stock', '重新编辑', 'index.php?app=store_stock&amp;act=edit&amp;storestock_id=' . $storestock_id);
        }
    }



    /**
     * 验证post提交
     * @return bool
     */
    private function checkPostParams()
    {
        // 逻辑
        // 如果是post提交
        // 活动名称不能为空
        if (empty(trim($_POST['arrival_time']))) {
            $this->show_warning('预计天数不能为空');
            return false;
        }

        // 类型不能为空
        if (empty(trim($_POST['stock_address']))) {
            $this->show_warning('退货地址');
            return false;
        }

        // 如果都验证通过了，返回true
        return true;
    }

}

?>
